/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2015
 */


package com.environment_pro.monitor.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.environment_pro.monitor.model.TbWarehouse;
import com.environment_pro.monitor.service.TbWarehouseService;
import com.environment_pro.monitor.util.StaticUtil;
import com.environment_pro.monitor.util.PageHelper;
import com.environment_pro.monitor.util.PageHelper.Page;

@Controller
@RequestMapping("/tbWarehouse")
public class TbWarehouseController extends BaseController {
	
	private static final Logger logger = Logger.getLogger(TbWarehouseController.class);

	@Autowired
	private TbWarehouseService tbWarehouseService;
	
	/**
	 * 获取所有列表 
	 * @param request
	 * @return
	 */
	@RequestMapping("/getAllTbWarehouse")
	public String getAllTbWarehouse(TbWarehouse tbWarehouse, HttpServletRequest request){
		//初始化分页 参数
		initPageParameter(request);
		//查询
		Page<TbWarehouse> mypage = tbWarehouseService.findAll(pageInfo,tbWarehouse);
		//返回查询条件和结果
		request.setAttribute("mypage", mypage);
		request.setAttribute("tbWarehouse", tbWarehouse);
		request.setAttribute("tbWarehouseList", mypage.getResult());
		//返回页面
		return "/TbWarehouse/allTbWarehouse";
	}
	
	/**
	 * 获取所有列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/getAllTbWarehouseJson")
	@ResponseBody
	public List<TbWarehouse> getAllTbWarehouseJson(TbWarehouse tbWarehouse,HttpServletRequest request){
		//初始化分页 参数
		initPageParameter(request);
		Page<TbWarehouse> mypage = tbWarehouseService.findAll(pageInfo,tbWarehouse);
		
		return mypage.getResult();
	}
	
	/**
	 * 跳转到添加界面
	 * @param request
	 * @return
	 */
	@RequestMapping("/toAddTbWarehouse")
	public String toAddTbWarehouse(HttpServletRequest request){
		
		return "/TbWarehouse/addTbWarehouse";
	}
	/**
	 * 添加并重定向
	 * @param tbWarehouse
	 * @param request
	 * @return
	 */
	@RequestMapping("/addTbWarehouse")
	public String addTbWarehouse(TbWarehouse tbWarehouse,HttpServletRequest request){
		tbWarehouseService.save(tbWarehouse);
		this.saveLogOperate(request,"");
		logger.info("");
		return "redirect:/tbWarehouse/getAllTbWarehouse";
	}
	
	/**
	 *编辑
	 * @param tbWarehouse
	 * @param request
	 * @return
	 */
	@RequestMapping("/updateTbWarehouse")
	public String updateTbWarehouse(TbWarehouse tbWarehouse,HttpServletRequest request){
		
		if(tbWarehouseService.update(tbWarehouse)){
			//tbWarehouse = tbWarehouseService.findById(tbWarehouse.getId());
			request.setAttribute("tbWarehouse", tbWarehouse);
			this.saveLogOperate(request,"");
			return "redirect:/tbWarehouse/getAllTbWarehouse";
		}else{
			return "/error";
		}
	}
	/**
	 * 根据id查询单个
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/getTbWarehouse")
	public String getTbWarehouse(int id,HttpServletRequest request){
		
		request.setAttribute("tbWarehouse", tbWarehouseService.findById(id));
		return "/TbWarehouse/editTbWarehouse";
	}
	
	/**
	 * 根据id查询单个
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/getTbWarehouseJson")
	@ResponseBody
	public TbWarehouse getTbWarehouseJson(int id,HttpServletRequest request){
		return tbWarehouseService.findById(id);
	}
	
	/**
	 * 删除
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/delTbWarehouse")
	public void delTbWarehouse(int id,HttpServletRequest request,HttpServletResponse response){
		String result = "{\"result\":\"error\"}";
		TbWarehouse tbWarehouse = tbWarehouseService.findById(id);
		if(tbWarehouseService.delete(id)){
			result = "{\"result\":\"success\"}";
		}
		this.saveLogOperate(request,tbWarehouse.toString());
		response.setContentType("application/json");
		this.saveLogOperate(request,"");
		try {
			response.getWriter().write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

